/*
	Copyright (C) 2008 Demiurg-HG
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
// win_net.h - network stuff

#pragma error do not include this file to project!

#pragma once


#include "..\global.h"
#include <winsock2.h>

#pragma comment (lib, "ws2_32.lib")


/*-----------------------------------------------------------------------------
	Network System :
-----------------------------------------------------------------------------*/

const char *NET_ErrorString(void);

class	EWinNetworkSystem;



class	EWinTCPServer : public INetServer
	{
	public:
					EWinTCPServer		( EWinNetworkSystem *ns );
					~EWinTCPServer		( void );
		
		void		Run					( word port );
		void		Kill				( void );
		bool		IsRunning			( void );
		
		uint		GetMaxClientNum		( void );
		bool		IsClientConnected	( uint cl );
		void		AcceptClient		( void );
		void		DisconnectClient	( uint cl, int err_code );
		void		SwitchClient		( uint cl );
		int			Recv				( void *data, uint size );
		int			Send				( void *data, uint size );
	private:			
		uint		current_client;
		IConsole	*con;			
		SOCKET		s_listen;
		SOCKET		clients[SV_MAXCLIENTS];
	};
	
	
class	EWinTCPClient : public INetClient
	{
	public:
					EWinTCPClient	( EWinNetworkSystem *ns );
					~EWinTCPClient	( void );

		void		Connect			( const char *addr, word port );
		void		Disconnect		( void );
		bool		IsConnected		( void );
		int			Send			( void *data, uint size );
		int			Recv			( void *data, uint size );	
	private:
		IConsole	*con;	
		SOCKET		s_client;		
	};



class	EWinNetworkSystem : public INetworkSystem
	{
	friend class EWinTCPServer;
	friend class EWinTCPClient;
	public:
							EWinNetworkSystem	( IConsole *con, ISystem *sys );
		virtual				~EWinNetworkSystem	( void );
		
		virtual	INetServer	*CreateServer		( void );
		virtual void		DestroyServer		( INetServer *net_sv );
		
		virtual INetClient	*CreateClient		( void );
		virtual void		DestroyClient		( INetClient *net_cl );
		
	private:
		//	General stuff :
		IConsole	*con;
		ISystem		*sys;
		WSADATA		wsa_data;
	};	
	
	
	
	
	
	
	
	
	
	
	
	